package com.tbvtc.agriculturalcreditchainserver2025.mapper;

import com.tbvtc.agriculturalcreditchainserver2025.dto.LandDto;
import com.tbvtc.agriculturalcreditchainserver2025.dto.farmer.registerCropDto;
import com.tbvtc.agriculturalcreditchainserver2025.entity.Crop;
import com.tbvtc.agriculturalcreditchainserver2025.pojo.landInit;
import com.tbvtc.agriculturalcreditchainserver2025.vo.farmerVo.FoodMessage;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FarmerMapper {
    @Select("select address from user where id=#{id}")
    String selectAddress(Integer id);

    @Insert("insert into crop(name,price, phone,crop_image,crop_category,land_name,create_time,is_preorder,is_sale) value (#{foodName}, #{price}, #{phone},#{cropImage},#{cropCategory},#{landName},#{createTime},#{isPreorder},#{isSale});")
    void insertCrop(Crop registerCropDto);

    @Select("select * from crop where phone=#{phone}")
    List<FoodMessage> selectFoodByPhone(String phone);

    @Select("select * from crop ;")
    List<FoodMessage> findAllCrop();

    @Update("update crop set is_mature=1 where name=#{foodName}")
    void ismatured(String foodName);

    @Insert("insert into farmland_info (role_id,landName, landsize, landQuality, climate, previousCro,is_active,land_image,land_address,is_planting,longitude,latitude,province_code,city_code,area_code,create_time) " +
            "values (#{id},#{landName},#{landsize},#{landQuality},#{climate},#{previousCro},#{isActive},#{landImage},#{landAddress},#{isPlanting},#{latitude},#{longitude},#{provinceCode},#{cityCode},#{areaCode},#{createTime});")
    void addLand(landInit landInit);

    @Select("select f.*,u.username from farmland_info f left join  user u on f.role_id=u.id where f.role_id=#{id};")
    List<LandDto> findAllLand(Integer id);

    @Select("select f.*,u.username from farmland_info f left join  user u on f.role_id=u.id where f.id=#{id};")
    landInit getLandById(String id);

    @Insert("update farmland_info set is_planting=1 where landName=#{LandName}")
    void isplanting(String LandName);

    @Select("select * from crop where id=#{CropId}")
    Crop selectCrop(Integer CropId);

    @Update("update farmland_info set is_active=#{isActive} where id=#{landId}")
    void markLandUsed(String landId, String isActive);

    @Select("select crop.land_name from crop where name=#{foodName}")
    String selectLandNameByFoodName(String foodName);

    @Update("update farmland_info set is_active=0 where landName=#{landName} ")
    void updateActiveByLandName(String landName);
}
